package com.example.newproject_overseas.components

import android.util.Log
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material.Divider
import androidx.compose.material.ExperimentalMaterialApi
import androidx.compose.material.Icon
import androidx.compose.material.ModalBottomSheetLayout
import androidx.compose.material.ModalBottomSheetState
import androidx.compose.material.Text
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.viewmodel.compose.viewModel
import com.example.newproject_overseas.ViewModel.JobViewModel
import com.example.newproject_overseas.model.entity.ModelItem
import kotlinx.coroutines.launch


//获取地址信息
@OptIn(ExperimentalMaterialApi::class)
@Composable
fun TwoLevelAddress(
    state: ModalBottomSheetState,
    provinces: List<ModelItem>,
    onItemSelected: (SelectedObject) -> Unit,
    onItemSelectId:(ModelItem) -> Unit
) {

    // Define your state variables here
    val scope = rememberCoroutineScope()
    var occ1Name by remember { mutableStateOf("") }
    var occ2Name by remember { mutableStateOf("") }
    var occ1Id by remember { mutableStateOf("") }
    var occ2Id by remember { mutableStateOf("") }
    var occVal by remember { mutableStateOf("") }
    var occBool by remember { mutableStateOf(false) }
    var selectedProvinceIndex by remember { mutableStateOf(-1) }
    var jobViewModel: JobViewModel = viewModel()




    //2级联动
    ModalBottomSheetLayout(
        sheetState = state,
        sheetContent = {

            Column(modifier = Modifier
                .height(250.dp),
                verticalArrangement = Arrangement.Center,
                horizontalAlignment = Alignment.CenterHorizontally) {
                //标题
                Row(modifier = Modifier
                    .fillMaxWidth(),
                    verticalAlignment = Alignment.CenterVertically,
                    horizontalArrangement = Arrangement.Center) {
                    Icon(
                        imageVector = Icons.Default.ArrowBack,
                        contentDescription = "Back",
                        tint = Color.Gray, // Icon color
                        modifier = Modifier
                            .clickable {
                                Log.e("图标", "Text: ", )
                                occBool = false; //关闭2级页
                            }
                            .padding(start = 16.dp)
                    )
                    Spacer(modifier = Modifier.weight(1f))
                    Text(
                        modifier = Modifier
                            .padding(top = 10.dp, bottom = 10.dp),
                        text = "Please choose",
                        fontSize = 20.sp,
                        color = Color(0xFFFFCC00)
                    )
                    Spacer(modifier = Modifier.weight(1.5f))
                }



                LazyColumn(modifier = Modifier
                    .height(250.dp)){
                    items(provinces) { data ->

                        //1级
                        if(occBool == false){
                            Divider()
                            Row(modifier = Modifier
                                .clickable {
                                    Log.i(data.toString(), "Text: ")
                                    occ1Name  = data.name;
                                    occ1Id    = data.id;
                                    occBool   = true;
                                    selectedProvinceIndex = provinces.indexOf(data)
                                    onItemSelectId(data);
                                }
                                .padding(top = 10.dp, bottom = 10.dp)
                                .fillMaxWidth(),
                                verticalAlignment = Alignment.CenterVertically,
                                horizontalArrangement = Arrangement.Center
                            ) {
                                Text(text = data.name)
                            }

                        }else if(selectedProvinceIndex == provinces.indexOf(data) && occBool == true){
                            //2级
                            jobViewModel.addressChilderModel?.forEach { city ->
                                Divider()
                                Row(modifier = Modifier
                                    .clickable {
                                        occ2Id   = city.id
                                        occ2Name = city.name;
                                        occVal   = occ1Name + occ2Name //最终结果
                                        occBool  = false;
                                        scope.launch { state.hide() }

                                        val selectedObject = SelectedObject(
                                            id1 = occ1Id,
                                            name1 = occ1Name,
                                            id2 = occ2Id,
                                            name2 = occ2Name,
                                            name = "$occ1Name$occ2Name"
                                        )


                                        onItemSelected(selectedObject)
                                    }
                                    .padding(top = 10.dp, bottom = 10.dp)
                                    .fillMaxWidth(),
                                    verticalAlignment = Alignment.CenterVertically,
                                    horizontalArrangement = Arrangement.Center
                                ) {
                                    Text(text = city.name)
                                }
                            }

                        }






                    }




                }

            }

        }
    ){}



}
